/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.libarae.client.threads;

import org.libarae.communication.ClientInterface;
import org.libarae.communication.request.BookSearchRequest;
import org.libarae.communication.response.BookSearchResponse;
import org.libarae.communication.response.Response;

/**
 *
 * @author Ben
 */
public class BookSearchThread extends Thread {

    private HasBookSearchThread mParent;
    private BookSearchRequest mRequest;

    public BookSearchThread(BookSearchRequest mRequest, HasBookSearchThread mParent)
    {
        this.mRequest = mRequest;
        this.mParent = mParent;
    }

    public void run()
    {
        Response response = null;

        try {
            response = ClientInterface.sendRequest(mRequest);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        if(response == null)
        {
            //TODO: GUI error message
            System.err.println("Error connecting to or receiving from server");
        }

        if(response.getStatus() == Response.STATUS_GOOD)
        {
            BookSearchResponse bsr = (BookSearchResponse)response;

            mParent.redisplayList(bsr.getResultBooks());
        }

        //TODO: Handle error response
    }
}
